package com.ning.weixin.controller;

import com.ning.weixin.dto.IndexDto;
import com.ning.weixin.entity.UserInfo;
import com.ning.weixin.service.ClockAddressService;
import com.ning.weixin.service.UserAddressService;
import com.ning.weixin.service.UserInfoService;
import com.ning.weixin.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "用户首页")
@RestController
@RequestMapping("/njxs/index")
public class IndexController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserAddressService userAddressService;

    @Autowired
    private ClockAddressService clockAddressService;

    @ApiOperation("首页展示")
    @RequestMapping(value = "/indexshow",method = RequestMethod.POST)
    public Map<String,Object> indexShow(@RequestBody IndexDto indexDto){
        String openId = indexDto.getOpenId();
        if (openId != null){
            UserInfo userInfo = userInfoService.findUserInfo(openId);
            Integer rank = userInfoService.scoreRanking(openId);
            Map<String,Object> map = new HashMap<>();
            map.put("headImgUrl",userInfo.getHeadImgUrl());
            map.put("score",userInfo.getScore());
            map.put("clockNum",userInfo.getClockNum());
            map.put("rank",rank);
            List<String> addressIdList = userAddressService.findByOpenId(openId);
            for (int i=0;i<addressIdList.size();i++){
                String addressId = addressIdList.get(i);
                Integer downClockNum = clockAddressService.getClockNumById(addressId);
                map.put("downClockNum" + i,downClockNum);
            }
            return map;
        }else {
           return R.error("获取信息失败");
        }
    }

}
